package com.morningstar.gateway.config;

import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import com.morningstar.common.result.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author fycx
 */
@Slf4j
@Configuration
public class SaTokenConfig {

    @Resource
    IgnoreUrlsConfig ignoreUrlsConfig;

    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
                // 拦截地址
                .addInclude("/**")
                // 开放地址
                .setExcludeList(ignoreUrlsConfig.getUrls())
                // 异常处理方法：每次setAuth函数出现异常时进入
                .setError(e -> {
                    log.error(ExceptionUtils.getStackTrace(e));
                    return Result.fail(e.getMessage());
                });
    }
}
